<html>
<head><meta charset="utf-8"><title>Source&lt;T&gt; refactoring · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html">Source&lt;T&gt; refactoring</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="175816402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816402">(Sep 16 2019 at 14:21)</a>:</h4>
<p>Let me try to talk this through, as it seems important.</p>



<a name="175816501"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816501" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816501">(Sep 16 2019 at 14:22)</a>:</h4>
<p>So, syntax trees in rust-analyzer are by designg value types. <code>1 + 1</code> in <code>foo.rs</code> looks exactly like <code>1 + 1</code> in <code>bar.rs</code>, and given that AST is reference counted, it's ok to intern syntax trees</p>



<a name="175816510"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816510" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816510">(Sep 16 2019 at 14:23)</a>:</h4>
<p>(and this even happens within a single file already)</p>



<a name="175816617"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816617" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816617">(Sep 16 2019 at 14:24)</a>:</h4>
<p>For analysis, you need to add some form of identity to the syntax trees.</p>



<a name="175816680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816680">(Sep 16 2019 at 14:24)</a>:</h4>
<p>for example, if you have <code>#[path = "foo.rs"] mod x; #[path = "foo.rs"] mod y; </code>, then the same syntax from <code>foo.rs</code> would represent two different modules</p>



<a name="175816710"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816710" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816710">(Sep 16 2019 at 14:25)</a>:</h4>
<p>it's module identity that distinguishes one <code>foo</code> from another.</p>



<a name="175816716"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816716" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816716">(Sep 16 2019 at 14:25)</a>:</h4>
<p>(I'm going to break this out into its own topic, it seems worth separating; also, I'm going to have to run for a bit, but I'll continue async)</p>



<a name="175816779"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816779" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816779">(Sep 16 2019 at 14:25)</a>:</h4>
<p>ok carry on <span class="user-mention" data-user-id="133169">@matklad</span>, I'm following so far :)</p>



<a name="175816851"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175816851" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175816851">(Sep 16 2019 at 14:26)</a>:</h4>
<p>So far, this jump from "value-type syntax" to "identity-based hir" was pretty adhoc</p>



<a name="175817021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817021">(Sep 16 2019 at 14:28)</a>:</h4>
<p>However, we had an <code>struct Source&lt;T&gt; { file_id: HirFileId, ast: T}</code> type just to conveniently describe a piece of ast in a file. It was used mostly by the IDE side of thing, so that you can go from <code>hir::Struct</code> (which is an id) to <code>Source&lt;ast::StructDef&gt;</code> which you can use in the editor for goto def (this is essentially a span: file name + text range inside a file)</p>



<a name="175817058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817058">(Sep 16 2019 at 14:28)</a>:</h4>
<p>And now, in the <a href="https://github.com/rust-analyzer/rust-analyzer/pull/1815" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/pull/1815">https://github.com/rust-analyzer/rust-analyzer/pull/1815</a>, it seems like this <code>Source&lt;T&gt;</code> thing is how we'd add a hygine</p>



<a name="175817153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817153">(Sep 16 2019 at 14:29)</a>:</h4>
<blockquote>
<p><code>struct Source&lt;T&gt; { file_id: HirFileId, ast: T}</code></p>
</blockquote>
<p>does this also include the line/column/whatever <em>within</em> the file?</p>



<a name="175817229"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817229" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817229">(Sep 16 2019 at 14:30)</a>:</h4>
<p>Yeah: currently each node in AST remembers position within a file</p>



<a name="175817268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817268">(Sep 16 2019 at 14:30)</a>:</h4>
<p>That is, this info will be present in <code>T</code> for reasonable <code>T</code>s</p>



<a name="175817391"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817391" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817391">(Sep 16 2019 at 14:31)</a>:</h4>
<p>So, using the <code>file_id</code> field of the <code>Source</code> you should be able to fetch hygiene tables from the database, and assign proper hygiene to identifiers in the AST</p>



<a name="175817464"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817464" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817464">(Sep 16 2019 at 14:32)</a>:</h4>
<p>this is super hand-wavy, as this is a naturally emerging abstraction, I am not sure about the details yet :)</p>



<a name="175817545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817545">(Sep 16 2019 at 14:33)</a>:</h4>
<p>This <code>Source&lt;T&gt;</code> is also used in <a href="https://github.com/rust-analyzer/rust-analyzer/pull/1853" target="_blank" title="https://github.com/rust-analyzer/rust-analyzer/pull/1853">https://github.com/rust-analyzer/rust-analyzer/pull/1853</a>, which cleanups the way IDE goes from offsets to hir.</p>



<a name="175817670"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817670" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817670">(Sep 16 2019 at 14:34)</a>:</h4>
<p>Basically, the problem in IDE is that if you have <code>#[path = "foo.rs"] mod x; #[path = "foo.rs"] mod y;</code> and the user asks for "Goto Definition" for some definition inside <code>foo.rs</code>, the request is ambiguous, as <code>foo.rs</code> is effectively included twice.</p>



<a name="175817681"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175817681" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175817681">(Sep 16 2019 at 14:34)</a>:</h4>
<p>The Source thing captures that ambiguity precisely</p>



<a name="175818529"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175818529" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jeremy Kolb <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175818529">(Sep 16 2019 at 14:42)</a>:</h4>
<p>Is <code>source_binder</code> going away?</p>



<a name="175818548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/Source%3CT%3E%20refactoring/near/175818548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/Source.3CT.3E.20refactoring.html#175818548">(Sep 16 2019 at 14:42)</a>:</h4>
<p>yeah</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>